package com.zsyh.jiujiu.fragment.magager;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.zyh.toolslib.base.BaseFragment;
import com.example.zyh.toolslib.utils.SPUtils;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.adepter.OnItemClickLitener;
import com.zsyh.jiujiu.adepter.SingleAdapter;
import com.zsyh.jiujiu.bean.MessageEvn;
import com.zsyh.jiujiu.bean.MessageOrderBean;
import com.zsyh.jiujiu.bean.MyGoodBean;
import com.zsyh.jiujiu.fragment.orderFragment.AllOrderFragment;
import com.zsyh.jiujiu.home.FaBuActivity;
import com.zsyh.jiujiu.net.MyFragementObserver;
import com.zsyh.jiujiu.net.MyObserver;
import com.zsyh.jiujiu.utils.BigDecimalUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class shangjiaFragment extends BaseFragment {

    @BindView(R.id.rvlist)
    RecyclerView rvlist;
    private BaseQuickAdapter<MyGoodBean.DataBean, BaseViewHolder> mAdapter;
    List<MyGoodBean.DataBean> list = new ArrayList<>();

    public static shangjiaFragment newInstance(Context contexts, int status) {
        Bundle args = new Bundle();
        args.putInt("status", status);
        shangjiaFragment fragment = new shangjiaFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_manager;
    }

    @Override
    protected void initView(View rootView) {
        EventBus.getDefault().register(this);
        mAdapter=new BaseQuickAdapter<MyGoodBean.DataBean, BaseViewHolder>(R.layout.item_shangjia,list) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, MyGoodBean.DataBean item) {
                String[] split = item.getGoodsPic().split("\\|");
                Glide.with(getActivity()).load(split[0]).into(helper.<ImageView>getView(R.id.huanrulogo));
                helper.setText(R.id.huanrugoodname,item.getGoodsTitle());
                helper.setText(R.id.money,item.getGoodsPrice()+"");
                helper.setText(R.id.kucun,"共"+item.getStockCount()+item.getGoodsUnit()+"商品 总价值：¥"+ BigDecimalUtil.decimalTwo(item.getGoodsPrice()*item.getStockCount()));
                helper.setVisible(R.id.bianji,false);
                helper.setOnClickListener(R.id.bianji, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundlefa = new Bundle();
                        bundlefa.putString("goodid", item.getId());
                        bundlefa.putString("userType", item.getUserType());
                        bundlefa.putString("goodsTitle", item.getGoodsTitle());
                        bundlefa.putString("goodsDesc", item.getGoogsDesc()+"");
                        bundlefa.putString("goodsPic", item.getGoodsPic());
                        bundlefa.putString("isNewGoods",item.getIsNewgoods());
                        bundlefa.putString("goodsClass", item.getGoodsClass());
                        bundlefa.putString("goodsClassTwo", item.getGoodsClassTwo());
                        bundlefa.putString("goodsClassThree", item.getGoodsClassThree());
                        bundlefa.putString("beginCount", item.getBeginCount()+"");
                        bundlefa.putString("stockCount", item.getStockCount()+"");
                        bundlefa.putString("shelfLife", item.getShelfLife());
                        bundlefa.putString("produntDate", item.getProductDate());
                        bundlefa.putString("goodsUnit", item.getGoodsUnit());
                        bundlefa.putString("goodsPrice", item.getGoodsPrice()+"");
                        bundlefa.putString("releaseProv", item.getReleaseProv());
                        bundlefa.putString("releaseProvname", item.getReleaseProvname());
                        bundlefa.putString("releaseCity", item.getReleaseCity());
                        bundlefa.putString("releaseCityname", item.getReleaseCityname());
                        bundlefa.putString("currentAddress", item.getCurrentAddress());
                        bundlefa.putString("exchangeClass", item.getExchangeClass());
                        bundlefa.putString("exchangeGoods", item.getExchangeGoods());
                        bundlefa.putString("lat", item.getLat());
                        bundlefa.putString("lon", item.getLon());
                        ActivityUtils.startActivity(bundlefa,FaBuActivity.class);
                    }
                });

                helper.setOnClickListener(R.id.shanchu, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        shanchu(item.getId());
                    }
                });

                helper.setOnClickListener(R.id.xiajia, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        xiajia(item.getId(),"2");
                    }
                });
            }
        };
        rvlist.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvlist.setAdapter(mAdapter);
        MyGood((String) SPUtils.get(getActivity(), "userid", ""),"1");
    }

    @Override
    protected void initData() {


    }

    @Override
    protected void initOnlyFirstLazy() {
        super.initOnlyFirstLazy();
        MyGood((String) SPUtils.get(getActivity(), "userid", ""),"1");

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvn messageInfo) {
        if (messageInfo.getMessage().equals("all")) {
            MyGood((String) SPUtils.get(getActivity(), "userid", ""),"1");
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }





    /**
     * 我的商品
     */
    public void MyGood(String userid,String stattus){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getMyGoods(userid,stattus)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(),true) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("wode",JsonStr);
                        Gson gson=new Gson();
                        MyGoodBean goodBean=gson.fromJson(JsonStr,MyGoodBean.class);
                        if(list!=null){
                            list.clear();
                        }
                        List<MyGoodBean.DataBean> nlist=new ArrayList<>();
                        for(int i=0;i<goodBean.getData().size();i++){
                            list.add(goodBean.getData().get(i));
                            nlist.add(goodBean.getData().get(i));
                        }
                        mAdapter.notifyDataSetChanged();
                        EventBus.getDefault().post(new MessageEvn(nlist.size()+"", "gaibian"));




                    }
                });

    }

    /**
     * xiajia
     */
    public void xiajia(String goodid,String status){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .updateGoodsStatus(goodid,status)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(),false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("wode",JsonStr);
                        MyGood((String) SPUtils.get(getActivity(), "userid", ""),"1");
                    }
                });


    }

    /**
     * 删除
     */
    public void shanchu(String doodid){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .delGoods(doodid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(),false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("wode",JsonStr);
                        MyGood((String) SPUtils.get(getActivity(), "userid", ""),"1");
                    }
                });
    }

}
